package com.www.applyword.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.ViewStub;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.adapter.MyJobAdapter;
import com.www.applyword.base.BaseFragment;
import com.www.applyword.bean.HotJobBean;
import com.www.applyword.bean.OrderBean;
import com.www.applyword.event.EventConstants;
import com.www.applyword.event.MessageEvent;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.ui.home.EmployedActivity;
import com.www.applyword.ui.home.HotJobDetailsActivity;
import com.www.applyword.utils.SPUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * author : huang
 * date   : 2019/10/22 0022 15:16
 * version: 1.0
 */
public class HaveReleaseFragment2 extends BaseFragment {
    @BindView(R.id.rl_job)
    RecyclerView rlJob;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refresh;

    ViewStub vsEmpty;

    private MyJobAdapter mHotJobAdapter;
    private List<HotJobBean> hotJobBeans = new ArrayList<>();

    private int page = 1;
    private int Rows = 10;
    private String url;

    @Override
    protected int getLayoutResource() {
        return R.layout.launched_fragment;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        vsEmpty =  rootView.findViewById(R.id.vs_empty);
        rlJob.setLayoutManager(new LinearLayoutManager(getActivity()));
        rlJob.setItemAnimator(new DefaultItemAnimator());
        mHotJobAdapter = new MyJobAdapter(getActivity());
        rlJob.setAdapter(mHotJobAdapter);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        showEmpty(true, vsEmpty, 0);
     //   initDate(true);
        refresh.setOnRefreshListener(refreshLayout -> initDate(true));
        refresh.setOnLoadMoreListener(refreshLayout ->initDate(false));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent messageEvent) {
        if (messageEvent.getMessageKey() != null) {
            if (messageEvent.getMessageKey().equals(EventConstants.PAY)) {
                initDate(true);
            }
        }
    }

    private void initDate(boolean isRefresh) {
      //  Constants.Order_Type = 1;
        if (isRefresh) page = 1;
        Map<String, Object> params = new HashMap<>();
        params.put("Page", page + "");
        params.put("Rows", Rows + "");
        params.put("UserID", SPUtil.getString(Constants.USERID));


        if (Constants.Order_Type == 10){
            params.put("State", 1 + "");
            url = Constants.GETMYORDERLIST;
        }else {
            params.put("State", 97 + "");
            url = Constants.GETEMPLOYMENTMEORDERLIST;
        }
        HttpClient.get(getActivity(),url, params, new CallBack<List<OrderBean>>() {
            @Override
            public void onSuccess(List<OrderBean> orderBeans) {
                if (isRefresh) {
                    refresh.finishRefresh(true);
                    if (orderBeans.size() == 0) {
                        showEmpty(true, vsEmpty, 0);
                    } else {
                        showEmpty(false, vsEmpty, 0);
                    }
                    mHotJobAdapter.replaceAll(orderBeans, true);
                } else {
                    if (orderBeans.size() > 0) {
                        refresh.finishLoadMore(true);
                        mHotJobAdapter.addAll(orderBeans);
                    } else {
                        refresh.finishLoadMoreWithNoMoreData();
                    }
                }
                page++;
            }
        });

        mHotJobAdapter.setOnItemClickListener((viewHolder, view, i) -> {
            if (Constants.Order_Type == 10){
                HaveReleaseFragment2.this.startActivity(new Intent(HaveReleaseFragment2.this.getActivity(), EmployedActivity.class)
                        .putExtra("OrderID", mHotJobAdapter.getItem(i).getOrder_ID()));
            }else {
                startActivity(new Intent(getActivity(), HotJobDetailsActivity.class)
                        .putExtra("OrderID", mHotJobAdapter.getItem(i).getOrder_ID()));
            }

           // startActivity(new Intent(this, EmployedActivity.class).putExtra("OrderID", order_id));
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}

